package de.mhus.lib.mandala.demo;

import java.util.List;

import org.codehaus.jackson.JsonNode;

import de.mhus.lib.mandala.Function;
import de.mhus.lib.mandala.Window;
import de.mhus.lib.mandala.attributes.EncapsulatedBooleanAttribute;
import de.mhus.lib.mandala.attributes.NamedAttribute;
import de.mhus.lib.mandala.attributes.NegativeBooleanAttribute;
import de.mhus.lib.mandala.bind.DataProvider;
import de.mhus.lib.mandala.bind.DataRow;
import de.mhus.lib.mandala.bind.DynamicDataSource;
import de.mhus.lib.mandala.bind.DynamicSingleDataSource;
import de.mhus.lib.mandala.bind.SingleDataProvider;
import de.mhus.lib.mandala.form.Checkbox;
import de.mhus.lib.mandala.form.EMailField;
import de.mhus.lib.mandala.form.Form;
import de.mhus.lib.mandala.form.Form.MODE;
import de.mhus.lib.mandala.form.MultipleSelect;
import de.mhus.lib.mandala.form.RadioSelect;
import de.mhus.lib.mandala.form.SingleSelect;
import de.mhus.lib.mandala.form.TextField;
import de.mhus.lib.mandala.functions.EncapsulatedFunctionCall;
import de.mhus.lib.mandala.functions.FunctionCall;
import de.mhus.lib.mandala.layout.HorizontalLayout;
import de.mhus.lib.mandala.layout.Panel;
import de.mhus.lib.mandala.list.ListItem;
import de.mhus.lib.mandala.list.SelectableList;
import de.mhus.lib.mandala.text.Text;
import de.mhus.lib.mandala.widgets.Button;

public class Demo02 extends Demo {

	protected void create(Window window) {
		
		HorizontalLayout layout = window.add(new HorizontalLayout());
		Panel panel = layout.add(new Panel("List"), new HorizontalLayout.Options(3));
		
		SelectableList list = panel.add(new SelectableList());
		ListItem item = list.setItem(new ListItem());
		Text heading = new Text(Text.TYPE.H4,"");
		heading.getTagAttributes().setBindValue(new NamedAttribute("name"));
		item.add(heading);

		DynamicDataSource userListData = new DynamicDataSource();
		list.getTagAttributes().setData(userListData.getTarget());
		userListData.setProvider(new DataProvider() {
			
			@Override
			public void fillWithData(JsonNode in, java.util.List<DataRow> data) {
				data.add(new DataRow("name", "Hans","id",1));
				data.add(new DataRow("name", "Peter","id",2));
				data.add(new DataRow("name", "Lars","id",3));
				data.add(new DataRow("name", "Sabine","id",4));
				data.add(new DataRow("name", "Max","id",5));
			} 
		});
		
		//list.setSelectAction(new EncapsulatedFunctionCall("data,event", new Function("alert(data);"), "(JSON.stringify(data));" ) );
		// item.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("data,event", "alert(JSON.stringify(data));" ) );

		window.getInitialAction().getActions().add(userListData);
		
		// ---
		
		Panel panelr = layout.add(new Panel("Form"), new HorizontalLayout.Options(9));

		Form form = panelr.add(new Form());
		form.setMode(MODE.READWRITE);
		
		form.add(new TextField("first", "First Name"));
		form.add(new TextField("last", "Last Name"));
		form.add(new EMailField("email", "E-Mail"));
		form.add(new Checkbox("customer", "Customer"));
		form.add(new SingleSelect("salutation", "Salutation", "mr", "Mr.", "mrs", "Mrs."));
		form.add(new MultipleSelect("products", "Products", "bread", "Bread", "milk", "Milk", "honey", "Honey"));
		form.add(new RadioSelect("customerClass", "Customer Class", "1", "A", "2", "B", "3", "C"));
		
		DynamicSingleDataSource formData = new DynamicSingleDataSource();
		form.getTagAttributes().setBindWith(formData.getTarget());
		formData.setProvider(new SingleDataProvider() {

			@Override
			public DataRow getData(JsonNode in) {
				if (in != null && in.has("name"))
					return new DataRow("id", in.get("id").getValueAsText(),"first", in.get("name").getValueAsText(), "last", "Hamster", "email", "hh@dada.de");
				else
					return new DataRow("first", "?", "last", "?", "email", "?");
			}
			
		});
	
		window.getInitialAction().getActions().add(formData);

		Button bDelete = new Button("Delete");
		bDelete.getTagAttributes().setBindVisible(new NegativeBooleanAttribute(form.getEditableSwitch()));

		Button bEdit = new Button("Edit");
//		bEdit.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("", form.getEditableSwitch(), "(!", form.getEditableSwitch(), "())" ));
		bEdit.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("", form.getEditableSwitch(), "(true)" ));
		bEdit.getTagAttributes().setBindVisible(new NegativeBooleanAttribute(form.getEditableSwitch()));
		
		Button bSave = new Button("Save");
		bSave.getTagAttributes().setBindVisible(form.getEditableSwitch());
		bSave.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("", form.getEditableSwitch(), "(false)" ));
		
		Button bCancel = new Button("Cancel");
		bCancel.getTagAttributes().setBindVisible(form.getEditableSwitch());
		bCancel.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("", form.getEditableSwitch(), "(false)" ));

		HorizontalLayout toolbar = window.add(new HorizontalLayout());
		
		toolbar.add(bDelete, new HorizontalLayout.Options(1));
		toolbar.add(bEdit, new HorizontalLayout.Options(1));
		toolbar.add(bSave, new HorizontalLayout.Options(1));
		toolbar.add(bCancel, new HorizontalLayout.Options(1));
		
		// ---
		
		// connect list and form
		
		item.getTagAttributes().setClickAction(new EncapsulatedFunctionCall("data,event", formData, "(data);" ) );
		item.getTagAttributes().createBindCss().getAttributes().put("active", new EncapsulatedBooleanAttribute("id == ", formData.getTarget(), "()['id']" ) );
		
		
	}
	
}
